﻿using mal.WP7.GoogleAnalytics.Business.Repository;

namespace mal.WP7.GoogleAnalytics.Business.GoogleApi
{
    public abstract class GoogleAnalyticsServiceBase : IGoogleAnalyticsService
    {
        #region Fields
        private bool isLoading;
        private string login;
        private string password;

        public event GetFeedCompletedEventHandler GetFeedCompleted;

        public event LoginCompletedEventHandler LoginCompleted;
        #endregion

        #region Construction / Destruction
        public GoogleAnalyticsServiceBase(string login, string password)
        {
            this.login = login;
            this.password = password;
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public bool IsLoading
        {
            get { return this.isLoading; }

            protected set { this.isLoading = value; }
        }

        protected string Login
        {
            get { return login; }
        }

        protected string Password
        {
            get { return password; }
        }
        #endregion

        #region Methods
        public void GetFeedAsync(FeedRequest request)
        {
            this.GetFeedAsync(request, null);
        }

        public void GetFeedDashboardDayAsync(Website webSite)
        {
            FeedRequest request = FeedRequest.GetFeedDashboardDay();
            request.WebSiteId = webSite.Id;

            this.GetFeedAsync(request);
        }

        public void GetFeedDashboardWeekAsync(Website webSite)
        {
            FeedRequest request = FeedRequest.GetFeedDashboardWeek();
            request.WebSiteId = webSite.Id;

            this.GetFeedAsync(request);
        }

        public void GetFeedDashboardMonthAsync(Website webSite)
        {
            FeedRequest request = FeedRequest.GetFeedDashboardMonth();
            request.WebSiteId = webSite.Id;

            this.GetFeedAsync(request);
        }

        public void GetFeedDashboardYearAsync(Website webSite)
        {
            FeedRequest request = FeedRequest.GetFeedDashboardYear();
            request.WebSiteId = webSite.Id;

            this.GetFeedAsync(request);
        }

        public void GetFeedDaysAsync(Website webSite)
        {
            FeedRequest request = FeedRequest.GetFeedDays();
            request.WebSiteId = webSite.Id;

            this.GetFeedAsync(request, webSite);
        }

        public void GetFeedWeeksAsync(Website webSite)
        {
            FeedRequest request = FeedRequest.GetFeedWeeks();
            request.WebSiteId = webSite.Id;

            this.GetFeedAsync(request, webSite);
        }

        public void GetFeedMonthsAsync(Website webSite)
        {
            FeedRequest request = FeedRequest.GetFeedMonths();
            request.WebSiteId = webSite.Id;

            this.GetFeedAsync(request, webSite);
        }

        public void LoginAsync()
        {
            this.LoginAsync(null);
        }

        public abstract void LoginAsync(object useState);

        public abstract void GetFeedAsync(FeedRequest request, object userState);

        protected void RaiseLoginCompletedEvent(LoginCompletedEventArgs e)
        {
            if (this.LoginCompleted != null)
            {
                this.LoginCompleted(this, e);
            }
        }

        protected void RaiseGetFeedCompletedEvent(GetFeedCompletedEventArgs e)
        {
            this.IsLoading = false;

            if (this.GetFeedCompleted != null)
            {
                this.GetFeedCompleted(this, e);
            }
        }
        #endregion

        #region Events

        #endregion
    }
}
